package servlet;

import pojo.teacher;
import service.schooltreasure_teacherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "teacherAddServlet", value = "/teacherAddServlet")
public class teacherAddServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        teacher teacher=new teacher();

        teacher.setTeacherName(req.getParameter("teacherName"));
        teacher.setUserName(req.getParameter("userName"));
        teacher.setPassword(Integer.parseInt(req.getParameter("password")));


        schooltreasure_teacherService schooltreasure_teacherService=new schooltreasure_teacherService();

        int i=0;
        try {
            i=schooltreasure_teacherService.addSchooltreasure_teacher(teacher);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        PrintWriter PrintWriter=resp.getWriter();
        if(i==1){
            PrintWriter.print("<script>alert('注册成功'); window.location='login.html' </script>");
        }else{
            PrintWriter.print("<script>alert('注册失败,存在重复名称'); window.location='register.html' </script>");
        }

        PrintWriter.flush();
        PrintWriter.close();



    }



}
